package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.ProductSkuProperty;
import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.service.ISkuPropertyValueService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wolfcode on 2018/8/18.
 */
@Controller
public class ProductSkuController {
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ISkuPropertyValueService propertyValueService;
    @Reference
    private IProductSkuService productSkuService;
    @RequestMapping("/productSku")
    public String productSkuPage(Long productId, Model model){
        Product product = productService.getProductById(productId);
        model.addAttribute("product", product);
        List<ProductSku> skuList = productSkuService.queryByProductId(productId);
        if(skuList.size()>0){
            List<SkuProperty> skuPropertyList = new ArrayList<SkuProperty>();
            for(ProductSkuProperty productSkuProperty:skuList.get(0).getProductSkuPropertyList()){
                skuPropertyList.add(productSkuProperty.getSkuProperty());
            }
            model.addAttribute("skuPropertyList",skuPropertyList);
            //已经生成过了SKU
            model.addAttribute("skuList",skuList);
            return "product_sku/sku_list";
        }
        model.addAttribute("skuPropertyList",skuPropertyService.queryByCatalogId(product.getCatalog().getId()));
        return "product_sku/generate_sku";
    }
    @RequestMapping("/productSku/getSkuPropertyValue")
    public String getSkuPropertyValuePage(Long skuPropertyId,Model model){
        model.addAttribute("skuProperty",skuPropertyService.get(skuPropertyId));
        model.addAttribute("skuPropertyValueList",propertyValueService.queryBySkuPropertyId(skuPropertyId));
        return "product_sku/sku_property_value_table";
    }
    @RequestMapping("/productSku/generateSku")
    public String generateSkuPage(@RequestBody GenerateSkuVo generateSkuVo,Model model){
        List<Map<String,Object>> resultData = skuPropertyService.generateSku(generateSkuVo);
        model.addAttribute("skuPropertyList",generateSkuVo.getSkuPropertyList());
        model.addAttribute("skuList",resultData);
        return "product_sku/generate_sku_page";
    }
    @RequestMapping("/productSku/save")
    @ResponseBody
    public JSONResultVo saveSku(GenerateSkuVo generateSkuVo){
        JSONResultVo vo = new JSONResultVo();
        try{
            productSkuService.saveSku(generateSkuVo);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
}
